package com.example.contact;

import com.google.protobuf.Any;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;

public class TestContact {
    public static void main(String[] args) throws IOException {
        Contact.Builder builder = Contact.newBuilder();
        try {
            builder.mergeFrom(new FileInputStream("src/main/java/com/example/contact/contact.bin"));
        } catch (IOException e) {
            System.out.println("文件不存在，创建一个新文件");
        }

        builder.addPeopleInfo(createPeople());

        FileOutputStream fileOutputStream = new FileOutputStream("src/main/java/com/example/contact/contact.bin");
        builder.build().writeTo(fileOutputStream);
        fileOutputStream.close();
    }

    private static PeopleInfo createPeople() {
        PeopleInfo.Builder peopleBuilder = PeopleInfo.newBuilder();
        System.out.println("----------添加联系人----------");
        Scanner in = new Scanner(System.in);

        System.out.print("姓名为：");
        String name = in.nextLine();
        peopleBuilder.setName(name);

        System.out.print("年龄为：");
        int age = in.nextInt();
        in.nextLine();
        peopleBuilder.setAge(age);

        while (true) {
            PeopleInfo.Phone.Builder builder = PeopleInfo.Phone.newBuilder();
            System.out.print("手机号为：");
            String phone = in.nextLine();
            if (phone.isEmpty()) {
                break;
            }

            builder.setPhone(phone);

            System.out.print("请输入手机类型（1、MP  2、TEL）：");
            int phoneType = in.nextInt();
            in.nextLine();
            switch (phoneType) {
                case 1:
                    builder.setPhoneType(PeopleInfo.Phone.PhoneType.MP);
                    break;
                case 2:
                    builder.setPhoneType(PeopleInfo.Phone.PhoneType.TEL);
                    break;
                default:
                    System.out.println("请输入正确的手机类型");
                    break;
            }

            peopleBuilder.addPhone(builder.build());
        }

        Address.Builder addressBuilder = Address.newBuilder();
        System.out.print("请输入家庭住址：");
        String home = in.nextLine();
        addressBuilder.setHomeAddress(home);

        System.out.print("请输入公司住址：");
        String company = in.nextLine();
        addressBuilder.setCompanyAddress(company);

        peopleBuilder.setData(Any.pack(addressBuilder.build()));


        System.out.print("请添加其他联系方式(1.QQ号    2.微信号)");
        int otherContact = in.nextInt();
        in.nextLine();
        switch (otherContact) {
            case 1:
                System.out.print("请输入QQ号：");
                String qq = in.nextLine();
                peopleBuilder.setQq(qq);
                break;
            case 2:
                System.out.print("请输入微信号：");
                String wechat = in.nextLine();
                peopleBuilder.setWechat(wechat);
                break;
            default:
                break;
        }

        while (true) {
            System.out.print("请输入备注标题：");
            String key = in.nextLine();
            if (key.isEmpty()) {
                break;
            }
            System.out.print("请输入备注内容：");
            String value = in.nextLine();
            peopleBuilder.putRemark(key, value);
        }


        return peopleBuilder.build();
    }
}
